﻿using System;
using Common.Entities;
using Common.Enums;
using DataAccess.Accessors.Interfaces;
using DataAccess.Converters;
using DataAccess.Repository.Interfaces;

namespace DataAccess.Repository
{
    public class SocialNetworkRepository : ISocialNetworkRepository
    {
        private readonly ISocialNetworkAccessor _socialNetworkAccessor;
        private readonly ISocialNetworkConverter _socialNetworkConverter;

        public SocialNetworkRepository(ISocialNetworkAccessor socialNetworkAccessor, ISocialNetworkConverter socialNetworkConverter)
        {
            _socialNetworkAccessor = socialNetworkAccessor;
            _socialNetworkConverter = socialNetworkConverter;
        }

        public SocialNetwork GetByNetworkId(long id)
        {
            var socialNetwork = _socialNetworkAccessor.GetNetworkById(id);
            return socialNetwork != null ? _socialNetworkConverter.ConvertToEntity(socialNetwork) : null;
        }

        public SocialNetwork GetByNetworkName(SocialNetworkName socialNetworkName)
        {
            var socialNetwork = _socialNetworkAccessor.GetNetworkByName(socialNetworkName.ToString());
            return socialNetwork != null ? _socialNetworkConverter.ConvertToEntity(socialNetwork) : null;
        }

        public long Save(SocialNetwork socialNetwork)
        {
            var socialNetworkDao = _socialNetworkConverter.ConvertToEntityDAO(socialNetwork);
            _socialNetworkAccessor.Save(socialNetworkDao);
            return socialNetworkDao.SocialNetworkId;
        }
    }
}
